<%@page import="com.ht.model.TaxiOrderModel"%>
<%@page import="com.ht.daoimp.TaxiOrderDaoImp"%>
<%@page import="com.ht.dao.TaxiOrderDao"%>
<%@page import="com.ht.model.ShopOrderModel"%>
<%@page import="com.ht.daoimp.ShopOrderDaoImp"%>
<%@page import="com.ht.dao.ShopOrderDao"%>
<%@page import="com.ht.daoimp.UserDaoImp"%>
<%@page import="com.ht.dao.UsersDao"%>
<%@page import="com.ht.model.UsersModel"%>
<%@page import="com.ht.model.OrderInfo"%>
<%@page import="com.ht.daoimp.OrderDaoImpl"%>
<%@page import="com.ht.dao.OrderDao"%>
<%@page import="java.io.BufferedOutputStream"%>
<%@page import="com.ht.utils.PayCommonUtil"%>
<%@page import="com.ht.pay.weixin.Utils.WeixinConfigUtils"%>
<%@page import="com.ht.utils.XMLUtil"%>
<%@page import="java.io.InputStreamReader"%>
<%@page import="java.io.BufferedReader"%>
<%@page import="java.io.InputStream"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	TaxiOrderDao taxiOrderDao=new TaxiOrderDaoImp();
	UsersDao userDao = new UserDaoImp();
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
	System.out.println("SUCCESS");
	//读取参数  
	InputStream inputStream;
	StringBuffer sb = new StringBuffer();
	inputStream = request.getInputStream();
	String s;
	BufferedReader in = new BufferedReader(new InputStreamReader(
			inputStream, "UTF-8"));
	while ((s = in.readLine()) != null) {
		sb.append(s);
	}
	in.close();
	inputStream.close();

	//解析xml成map  
	Map<String, String> m = new HashMap<String, String>();
	m = XMLUtil.doXMLParse(sb.toString());

	//过滤空 设置 TreeMap  
	SortedMap<Object, Object> packageParams = new TreeMap<Object, Object>();
	Iterator it = m.keySet().iterator();
	while (it.hasNext()) {
		String parameter = (String) it.next();
		String parameterValue = m.get(parameter);

		String v = "";
		if (null != parameterValue) {
			v = parameterValue.trim();
		}
		packageParams.put(parameter, v);
	}

	// 账号信息  
	String key = WeixinConfigUtils.Key; // key  

	//判断签名是否正确  
	if (PayCommonUtil.isTenpaySign("UTF-8", packageParams, key)) {
		//------------------------------  
		//处理业务开始  
		//------------------------------  
		String resXml = "";
		if ("SUCCESS".equals((String) packageParams.get("result_code"))) {
			// 这里是支付成功  
			System.out.println("支付成功");
			//通知微信.异步确认成功.必写.不然会一直通知后台.八次之后就认为交易失败了.  
			resXml = "<xml>"
					+ "<return_code><![CDATA[SUCCESS]]></return_code>"
					+ "<return_msg><![CDATA[OK]]></return_msg>"
					+ "</xml> ";
			BufferedOutputStream out1 = new BufferedOutputStream(
					response.getOutputStream());
			out1.write(resXml.getBytes());
			out1.flush();
			out1.close();
			//////////执行自己的业务逻辑////////////////  
			String mch_id = (String) packageParams.get("mch_id");
			String openid = (String) packageParams.get("openid");
			String is_subscribe = (String) packageParams
					.get("is_subscribe");
			String out_trade_no = (String) packageParams
					.get("out_trade_no");

			String total_fee = (String) packageParams.get("total_fee");

			String attach = (String) packageParams.get("attach");

			System.out.println("mch_id:" + mch_id);
			System.out.println("openid:" + openid);
			System.out.println("is_subscribe:" + is_subscribe);
			System.out.println("out_trade_no:" + out_trade_no);
			System.out.println("total_fee:" + total_fee);
			System.out.println("attach:" + attach);
			taxiOrderDao.updateByOutTradeNoTaxiorderType(attach, "4");

			TaxiOrderModel taxiOrderModel = taxiOrderDao.getByOutTradeNoOrderInfo(attach);
			UsersModel userModel = userDao.getByIdUserInfo(taxiOrderModel
					.getDriverId() + "");
			String fee = userModel.getRemainingsum();
				fee = (Double.parseDouble(userModel.getRemainingsum()) + Double
						.parseDouble(taxiOrderModel.getPrice())) + "";
				userDao.updateRemainingsum(taxiOrderModel.getDriverId() + "", fee);
		} else {
			System.out.println("支付失败,错误信息："
					+ packageParams.get("err_code"));
			resXml = "<xml>"
					+ "<return_code><![CDATA[FAIL]]></return_code>"
					+ "<return_msg><![CDATA[报文为空]]></return_msg>"
					+ "</xml> ";
			BufferedOutputStream out1 = new BufferedOutputStream(
					response.getOutputStream());
			out1.write(resXml.getBytes());
			out1.flush();
			out1.close();
		}
		//------------------------------  
		//处理业务完毕  
		//------------------------------  
	} else {
		System.out.println("通知签名验证失败");
	}
%>
